package com.amlyc.syn;

public class UnsafeBank {
    public static void main(String[] args) {
        Account account = new Account(1000, "结婚基金");
        Drawing you = new Drawing(account, 50, "you");
        Drawing girl = new Drawing(account, 100, "girl");
        you.start();
        girl.start();
    }
}
class Account{
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }

}
class Drawing extends Thread{

    Account account;
    int drawingMoney;
    int nowMoney=0;

    public Drawing(Account account, int drawingMoney,String name){
        super(name);
        this.account=account;
        this.drawingMoney=drawingMoney;

    }

    @Override
    public void run(){


        synchronized (account) {
            if (account.money - drawingMoney < 0) {
                System.out.println("money is done");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.money -= drawingMoney;
            nowMoney += drawingMoney;
            System.out.println("accountMoney:" + account.money);
            System.out.println(Thread.currentThread().getName()+"nowMoney:"+nowMoney);

        }
    }
}
